package org.wak.gui.sci
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	
	import org.wak.action.actions.AdventureActions;
	import org.wak.gui.events.GuiEvent;
	import org.wak.inventory.Inventory;
	import org.wak.inventory.InventoryItem;
	import org.wak.inventory.events.InventoryEvent;
	import org.wak.utils.IIterator;
	import org.wak.utils.WakSimpleButton;
	
	
  /**
	* Dispatched when an action button is clicked
	* inside the inventory window.
	* 
	* @eventType org.wak.gui.events.GuiEvent.ACTION_SELECTED
	*/
	[Event(name="ACTION_SELECTED", type="org.wak.gui.events.GuiEvent")]
	
  /**
	* Dispatched when an inventory item button is selected
	* in the inventory window.
	* 
	* @eventType org.wak.gui.events.GuiEvent.INVENTORY_ITEM_SELECTED
	*/
	[Event(name="INVENTORY_ITEM_SELECTED", type="org.wak.gui.events.GuiEvent")]
	
	

	/**
	 * Class to render the player's inventory in the SCI gui. 
	 * 
	 */	
	public class SciInventoryDisplay extends Sprite
	{
		// rows of items to display
		protected var mRows:int = 0 ;
		// columns of items to display
		protected var mCols:int = 0 ;
		
		// width of each item in pixels
		protected var mItemWidth:int = 0 ;
		
		// height of each item in pixels
		protected var mItemHeight:int = 0 ;
		
		// pixels which separates the item rows
		protected var mRowsSeparator:int = 2 ;
		
		// pixels which separates the item columns
		protected var mColsSeparator:int = 1 ;
		
		// the inventory to render.
		protected var mInventory:Inventory = null ;
		
		// arrow up to scroll the inventory.
		protected var mArrowUp:WakSimpleButton = null ;
		
		// Arrow down to scroll the inventory.
		protected var mArrowDown:WakSimpleButton = null ;
		
		protected var mItemsArray:Array = null ;
		
		// Asoc array from Sprite reference to InventoryItem.
		protected var mSpriteToItem:Array = null ;
		
		// starting x to draw the items.
		protected var mItemsX:Number = 0 ;
		// starting y to draw the items.
		protected var mItemsY:Number = 0 ;
		
		protected var mInventoryMask:Sprite = null ;
		
		// the mouse selected item by over or click.
		protected var mSelectedItem:Sprite = null;
		
		protected var mLastActionDispatched:String = "" ;
		
		// layer where the items are added to easily handle the scroll.
		protected var mScrollingLayer:Sprite = null ;
		
		protected var mTotalRows:int = 0;
		
		// background DisplayObject.
		protected var mBackground:DisplayObject = null ;
		
		// the BG used if mBackground is null.
		protected var mDrawnBg:Sprite = null ;
		
		// background color used only if mBackground is null.
		protected var mBgColor:uint = 0xFFFFFF ;
		
		// border color used only if mBackground is null.
		protected var mBorderColor:uint = 0x000000 ;
		
		// background width used only if mBackground is null.
		protected var mBgWidth:Number = 50 ;
		
		// background height used only if mBackground is null.
		protected var mBgHeight:Number = 50 ;
		
		// shadow filter.
		protected var mShadowFilter:DropShadowFilter = null ;
		
		// Actions Buttons horizontal separator.
		private var mHorizBtnsSeparator:int = 5 ;
		
		// ok button
		private var mOkButton:WakSimpleButton = null ;
		
		// select button
		private var mSelectButton:WakSimpleButton = null ;
		
		// array of Actions button included in the inventory.
		private var mActionsArray:Array = null ;
		
		// array of buttons for actions.
		private var mActionButtonsArr:Array = null ;
		
		// local reference to the gui mouse cursor
		private var mMouseCursor:SciCursor = null ;
		
		// Enable or disable the item selection to use another action.
		private var mSelectingItem:Boolean = true ;
		
		private var mUseHorizontalLayout:Boolean = true ;
		
		private var mHorizontalLayoutLeftMargin:Number = 10 ;
		
		private var mHorizontalLayoutY:Number = 10 ;
		
		// flag to know if an item was selected since the inventory was opened.
		private var mItemWasSelected:Boolean = false ;
		
		
		
		
		/**
		 * Constructor 
		 * @param pInventory the inventory of the player's character
		 * @param pActionsArr Array of actions id to include in the inventory buttons.
		 * @param pRows visible rows that the inventory will have on screen.
		 * @param pCols visible columns that the inventory will have on screen.
		 * @param pItemWidth max width in pixels of an item on the inventory
		 * @param pItemHeight max height in pixels of an item on the inventory
		 * @param pBg DisplayObject to use as background.
		 * @param pBgColor Color to use as background in the case of pBg is null.
		 * @param pBorderColor Color to use in the border in the case of pBg is null.
		 * @param pW Inventory display width.
		 * @param pH Inventory display height.
		 * 
		 */		
		public function SciInventoryDisplay(pInventory:Inventory, pActionsArr:Array, pRows:int = 2, pCols:int = 4, 
						pItemWidth:int = 70, pItemHeight:int = 44, pBg:DisplayObject = null, 
						pBgColor:uint = 0xFFFFFF, pBorderColor:uint = 0x000000, pW:Number = 200, pH:Number = 200)
		{
			super();
			mInventory = pInventory ;
			mActionsArray = pActionsArr ;
			mRows = pRows ;
			mCols = pCols ;
			mItemWidth = pItemWidth ;
			mItemHeight = pItemHeight ;
			mInventory = pInventory ;
			mBackground = pBg ;
			mBgColor = pBgColor ;
			mBorderColor = pBorderColor ;
			mBgWidth = pW ;
			mBgHeight = pH ;
			
			
			// Initialize background.
			if( mBackground == null )
			{
				mDrawnBg = new Sprite();
				
				mDrawnBg.graphics.beginFill( mBgColor );
				mDrawnBg.graphics.drawRoundRect(0,0, mBgWidth, mBgHeight,20,20);
				mDrawnBg.graphics.endFill();
				
				mDrawnBg.graphics.lineStyle(3, mBorderColor );
				mDrawnBg.graphics.drawRoundRect(-1,-1, mBgWidth + 2, mBgHeight + 2,20,20);
				mDrawnBg.graphics.endFill();
				
				mDrawnBg.graphics.lineStyle(1, mBorderColor );
				mDrawnBg.graphics.drawRoundRect(4,4, mBgWidth - 8, mBgHeight - 8, 20, 20);
				mDrawnBg.graphics.endFill();
				
				mBackground = mDrawnBg ; 
				
			}
			this.addChild(mBackground);
			
			
			
			mScrollingLayer = new Sprite
			this.addChild(mScrollingLayer);
			
			mItemsArray = new Array();
			mSpriteToItem = new Array();
			
			
			addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage );
		}
		
		
		/**
		 * Overrides the pActionsArr passed in the constructor with new actions
		 * and set the buttons for that actions. 
		 * @param pBtnsArray Array with WakButtons for the actions.
		 * 
		 */		
		public function SetActionsButtons(pBtnsArray:Array):void
		{
			mActionButtonsArr = pBtnsArray ;
		}
		
		/**
		 * Use the horizontal layout for the row of buttons. 
		 * @param value Boolean.
		 * 
		 */		
		public function set horizontalLayout(value:Boolean):void
		{
			mUseHorizontalLayout = value ;
		}
		
		/**
		 * The horizontal space between inventory's buttons. 
		 * @param value Horizontal space in pixels.
		 * 
		 */		
		public function set buttonsHorizontalSpace(value:Number):void
		{
			mHorizBtnsSeparator = value ;
		}
		
		/**
		 * The left margin for the horizontal layout.  
		 * @param value Horizontal space for the margin in pixels.
		 * 
		 */		
		public function set horizontalLayoutLeftMargin(value:Number):void
		{
			mHorizontalLayoutLeftMargin = value ;
		}
		
		/**
		 * The vertical coordinate of the row of buttons. 
		 * @param value Y coordinate for the buttons.
		 * 
		 */		
		public function set horizontalLayoutY(value:Number):void
		{
			mHorizontalLayoutY = value ;
		}
		
		/**
		 * Enable or disable the inventory's shadow. 
		 * @param value boolean.
		 * 
		 */		
		public function set dropShadow(value:Boolean):void
		{
			if(value == true)
			{
				if(mShadowFilter == null)
					mShadowFilter = new DropShadowFilter(8, 45, 0x000000, 1, 10, 10, 1, BitmapFilterQuality.LOW, false, false, false);
				mBackground.filters = [mShadowFilter] ;
			}		
			else
			{
				mBackground.filters = null ;
			}	
		}
		
		/**
		 * Set the cursor used to show the selected items
		 * in the inventory. 
		 * @param value an SciCursor.
		 * 
		 */		
		public function set mouseCursor(value:SciCursor):void
		{
			mMouseCursor = value ;
		}
		
		
		/**
		 * Setter for the ok button of the inventory. 
		 * @param value the new WakSimpleButton
		 * 
		 */		
		public function set buttonOk(value:WakSimpleButton):void
		{
			mOkButton = value ;
		}
		
		/**
		 * Setter for the select arrow button of the inventory. 
		 * @param value the new WakSimpleButton
		 * 
		 */		
		public function set buttonSelect(value:WakSimpleButton):void
		{
			mSelectButton = value ;
		}
		
		
		/**
		 * Button to scroll up the inventory. 
		 * @param value
		 * 
		 */		
		public function set arrowUp(value:WakSimpleButton):void
		{
			mArrowUp = value ;
		}
		
		/**
		 * Button to scroll down the inventory.
		 * @param value
		 * 
		 */		
		public function set arrowDown(value:WakSimpleButton):void
		{
			mArrowDown = value ;
		}
		
		/**
		 * Set the horizontal coordinate for the first item of the inventory 
		 * @param value x coordinate.
		 * 
		 */		
		public function set firstItemX(value:Number):void
		{
			mItemsX = value ;
		}
		
		/**
		 * Set the verticall coordinate for the first item of the inventory 
		 * @param value y coordinate.
		 * 
		 */	
		public function set firstItemY(value:Number):void
		{
			mItemsY = value ;
		}
		
		/**
		 * True if an item was selected on an inventory session (since the inventory was open). 
		 * @return 
		 * 
		 */		
		public function get itemWasSelected():Boolean
		{
			return mItemWasSelected;
		}
		
		protected function CreateDefaultActionButtons( pActionsArr:Array ):Array
		{
			var fButtonsArray:Array = new Array();
				
			var fNewButton:WakSimpleButton = null ;
			for each( var action:String in pActionsArr )
			{
				fNewButton = CreateActionButton( action );
				fButtonsArray.push(fNewButton);
			}
			return fButtonsArray ;
		}
		
		protected function OnAddedToStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, OnAddedToStage );
			mSelectingItem = true ;
			
			if(mActionButtonsArr == null)
				mActionButtonsArr = CreateDefaultActionButtons( mActionsArray  ) ;	
			
			var fCurrentX:Number = mHorizontalLayoutLeftMargin ;
			for each( var button:WakSimpleButton in mActionButtonsArr)
			{
				//trace(button);
				this.addChild(button);
				if(mUseHorizontalLayout)
				{
					button.y = mHorizontalLayoutY ;
					button.x = fCurrentX ;
				}
				fCurrentX += button.width + mHorizBtnsSeparator ;
				if( button.buttonName != SciGui.CLOSE_INVENTORY )
				{
					button.addEventListener(MouseEvent.MOUSE_UP, OnActionSelected);
				}
				
				if(button.buttonName == SciGui.SELECT_INVENTORY)
					mSelectButton = button ;
					
				if(button.buttonName == SciGui.CLOSE_INVENTORY)
					mOkButton = button ;
					
			}
			var fLastBtn:WakSimpleButton = mActionButtonsArr[mActionButtonsArr.length -1] ;
			
			if(mSelectButton == null)
			{
				mSelectButton = CreateSelectButton();
				if(fLastBtn != null )
				{
					mSelectButton.y = fLastBtn.y ;
					mSelectButton.x = fLastBtn.x + fLastBtn.width + mHorizBtnsSeparator ;
				} 
				else
				{
					mSelectButton.y = 10 ;
					mSelectButton.x = 10 ;
					fLastBtn = mSelectButton ;
				}
				mSelectButton.addEventListener(MouseEvent.MOUSE_UP,OnActionSelected);
			}
			this.addChild(mSelectButton);
			
			if(mOkButton == null)
			{
				mOkButton = CreateOkButton();
				mOkButton.y = fLastBtn.y ;
				mOkButton.x = mSelectButton.x + mSelectButton.width + mHorizBtnsSeparator; 
			}
			this.addChild(mOkButton);
			mOkButton.addEventListener(MouseEvent.MOUSE_UP, OnButtonOkPressed);
			
			
			
			var fBtnSeparator:int = 12 ;
			// Create the scrolling buttons
			if(mArrowDown == null)
			{
				mArrowDown = CreateDownArrow();
				mArrowDown.x = mBackground.width - mArrowDown.width - fBtnSeparator ;
				mArrowDown.y = mBackground.height - mArrowDown.height - fBtnSeparator ;
			}
			addChild(mArrowDown);
			
			
			
			mArrowDown.addEventListener(MouseEvent.MOUSE_UP, OnScrollDown );
				
			if(mArrowUp == null)
			{
				mArrowUp = CreateUpArrow();
				mArrowUp.x = mArrowDown.x ;
				mArrowUp.y = mArrowDown.y - mArrowUp.height - fBtnSeparator ;
			}
			addChild(mArrowUp);
			
			
			
			mArrowUp.addEventListener(MouseEvent.MOUSE_UP, OnScrollUp );
			
			//mItemsX = 10 ;
			//mItemsY = mItemHeight ;
			
			// Set the scrolling mask
			if(mInventoryMask == null)
			{
				mInventoryMask = new Sprite() ;
				mInventoryMask.graphics.beginFill(0xFFFFFF);
				mInventoryMask.graphics.drawRect(0,0,mArrowUp.width + mCols * (mItemWidth + mColsSeparator), (mRows) * ( mItemHeight + mRowsSeparator) );
				mInventoryMask.graphics.endFill() ;
				mInventoryMask.x = mItemsX ;
				mInventoryMask.y = mItemsY ;
				this.addChild(mInventoryMask);
				mScrollingLayer.mask = mInventoryMask ;
			}
			
			// fill the inventory
			FillInventory( mInventory.GetItems() );
			
			// enable or disable scrolling buttons.
			if( (mItemsArray.length / mCols) > mRows )
			{
				mArrowDown.enabled = true ;
			}
			
			mInventory.addEventListener(InventoryEvent.ITEM_ADDED, OnItemAdded );
			mInventory.addEventListener(InventoryEvent.ITEM_REMOVED,OnItemRemoved );
			
			mItemWasSelected = false ;
			//this.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			this.addEventListener(Event.REMOVED_FROM_STAGE, OnRemovedToStage );
		}
		
		protected function OnRemovedToStage(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE, OnRemovedToStage );
			this.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			
			for each( var item:Sprite in mItemsArray)
			{
				item.addEventListener(MouseEvent.MOUSE_UP, OnItemMouseClick );
			}
			
			mOkButton.removeEventListener(MouseEvent.MOUSE_UP, OnButtonOkPressed);
			
			mSelectButton.removeEventListener(MouseEvent.MOUSE_UP,OnActionSelected);
			
			for each( var button:WakSimpleButton in mActionButtonsArr)
			{
				button.removeEventListener(MouseEvent.MOUSE_UP, OnActionSelected);
			}
			
			mInventory.removeEventListener(InventoryEvent.ITEM_ADDED, OnItemAdded );
			mInventory.removeEventListener(InventoryEvent.ITEM_REMOVED,OnItemRemoved );
			this.addEventListener(Event.ADDED_TO_STAGE, OnAddedToStage );
		}
		
		protected function OnButtonOkPressed(event:Event):void
		{
			var fEvent:GuiEvent = new GuiEvent( GuiEvent.INVENTORY_CLOSED );
			this.dispatchEvent(fEvent);
		}
		
		protected function OnMouseMove(event:MouseEvent):void
		{
			mOkButton.Update(event.stageX, event.stageY);
			mSelectButton.Update(event.stageX, event.stageY);
		}
		
		protected function OnItemMouseOver(event:MouseEvent):void
		{
			
			if(mSelectedItem != event.target )
			{
				mSelectedItem = event.target as Sprite;
				//var fItem:InventoryItem = mSpriteToItem[mSelectedItem.name] as InventoryItem;
				//trace("OnItemMouseOver "+ fItem.interactableId );
				//var fEvent:GuiEvent = new GuiEvent( GuiEvent.INVENTORY_ITEM_OVER,"",fItem);
				//this.dispatchEvent( fEvent );
			}
		}
		
		protected function OnItemMouseOut(event:MouseEvent):void
		{
			if( !mSelectedItem.hitTestPoint( event.stageX, event.stageY, true) )
			{
				var fTarget:Sprite = event.target as Sprite;
				var fItem:InventoryItem = mSpriteToItem[fTarget.name] as InventoryItem;
				//var fEvent:GuiEvent = new GuiEvent( GuiEvent.INVENTORY_ITEM_OUT,"",fItem);
				//this.dispatchEvent( fEvent );
				
				mSelectedItem = null ;
			}
		}
		
		protected function OnItemMouseClick(event:MouseEvent):void
		{
			//trace("SciInventory OnItemMouseClick:"+mSelectedItem.name)
			
			
			mSelectedItem = event.target as Sprite;
			
			var fItem:InventoryItem = mSpriteToItem[mSelectedItem.name] as InventoryItem;
			if(mSelectingItem)
			{
				//trace("Selecting")
				// dispatch the item selection event.
				
				
				// set the mouse cursor with the item's icon.
				mMouseCursor.SetActiveIcon(fItem.itemIconDisplay,true);
				mSelectingItem = false ;
				// dispatch the action use for this selected item
				DispatchAction( AdventureActions.ActionIdUse,fItem );	
				mItemWasSelected = true ;
			}
			else
			{
				//trace("NOT  Selecting")
				// dispatch the action use for this selected item
				var fEvent:GuiEvent = new GuiEvent( GuiEvent.INVENTORY_ITEM_SELECTED,"",fItem);
				this.dispatchEvent( fEvent );
				return ;	
			}
			
			
		
		}
		
		protected function FillInventory( pItItems:IIterator ):void
		{
			var fNewItem:InventoryItem = null ;
			var fItemDisplay:Sprite = null ;
			
			var fCurrentX:Number = mItemsX ;
			var fCurrentY:Number = mItemsY ;
			
			var fRowCounter:int = 0 ;
			var fColCounter:int = 0 ;
			
			while(pItItems.hasNext())
			{
				fNewItem = pItItems.next() as InventoryItem ;
				fItemDisplay = fNewItem.itemDisplay ;
				fItemDisplay.x = mItemsX + (fColCounter * mItemWidth) + mColsSeparator ; 
				fItemDisplay.y = mItemsY + (fRowCounter * mItemHeight) + mRowsSeparator ;
				
				mScrollingLayer.addChild(fItemDisplay);
				mItemsArray.push( fItemDisplay );
				mSpriteToItem[fItemDisplay.name] = fNewItem ;
				fItemDisplay.addEventListener(MouseEvent.MOUSE_OVER, OnItemMouseOver );
				fItemDisplay.addEventListener(MouseEvent.MOUSE_OUT, OnItemMouseOut );
				fItemDisplay.addEventListener(MouseEvent.MOUSE_UP, OnItemMouseClick );
				
				fColCounter++ ;
				if( fColCounter >= mCols )
				{
					fRowCounter++ ;
					fColCounter = 0 ;
				}
				
				
			}
			
			mTotalRows = mItemsArray.length / mCols ;
			
		}
		
		protected function OnItemAdded(event:InventoryEvent):void
		{
	
			var fNewItem:InventoryItem = event.inventoryItem ;
			
			mItemsArray.push( fNewItem.itemDisplay );
			mSpriteToItem[fNewItem.itemDisplay.name] = fNewItem ;
			
			RefreshItemsPositions( mItemsArray );
			
			var fNewItemRow:int = (mScrollingLayer.y + fNewItem.itemDisplay.y) / (mItemHeight + mRowsSeparator) ;
			
			//trace("OnItemAdded:"+ fNewItemRow );
			
			fNewItem.itemDisplay.addEventListener(MouseEvent.MOUSE_OVER, OnItemMouseOver );
			fNewItem.itemDisplay.addEventListener(MouseEvent.MOUSE_OUT, OnItemMouseOut );
			fNewItem.itemDisplay.addEventListener(MouseEvent.CLICK, OnItemMouseClick );
			
			var fItemBottomLine:Number = this.y + mScrollingLayer.y + fNewItem.itemDisplay.y  ;
			var fInventoryBottomLine:Number = this.y + (mRows * mItemHeight) ;
			
			while(fItemBottomLine > fInventoryBottomLine )
			{
				//trace("while OnItemAdded:"+ fNewItemRow );
				ScrollDown();
				UpdateScrollButtons(); 
				fItemBottomLine = this.y + mScrollingLayer.y + fNewItem.itemDisplay.y  ;
			}
			
			
		}
		
		protected function OnItemRemoved(event:InventoryEvent):void
		{
			var fRemovedItem:InventoryItem = event.inventoryItem ;
			var fIndex:int = mItemsArray.indexOf(fRemovedItem.itemDisplay);
			
			if(fIndex >= 0)
			{
				mItemsArray.splice(fIndex,1);
				mSpriteToItem[fRemovedItem.itemDisplay.name] = null ;
				if(mScrollingLayer.contains( fRemovedItem.itemDisplay ) )
					mScrollingLayer.removeChild( fRemovedItem.itemDisplay);
					
				fRemovedItem.itemDisplay.removeEventListener(MouseEvent.MOUSE_OVER, OnItemMouseOver );
				fRemovedItem.itemDisplay.removeEventListener(MouseEvent.MOUSE_OUT, OnItemMouseOut );
				fRemovedItem.itemDisplay.removeEventListener(MouseEvent.CLICK, OnItemMouseClick );
				
				RefreshItemsPositions( mItemsArray );
			}
		}
		
		protected function RefreshItemsPositions(pItemsArray:Array):void
		{
			var fRowCounter:int = 0 ;
			var fColCounter:int = 0 ;
			
			var fFirstItem:Sprite = mItemsArray[0] as Sprite ;
			
			for each( var item:Sprite in pItemsArray)
			{
				item.x = mItemsX + (fColCounter * mItemWidth) + mColsSeparator ; 
				item.y = mItemsY + (fRowCounter * mItemHeight) + mRowsSeparator ;
				
				if( !mScrollingLayer.contains(item) )
					mScrollingLayer.addChild(item);
					
				fColCounter++ ;
				if( fColCounter >= mCols )
				{
					fRowCounter++ ;
					fColCounter = 0 ;
				}
				
			}				
		}
		
		
		private var mArrowColor:uint = 0xFFFFFF ;
		private var mArrowBGColor:uint = 0x090909 ;
		
		protected function CreateUpArrow():WakSimpleButton
		{
			var fBtn:WakSimpleButton = null 
			
			
			 
			var fUp:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			fUp.rotation = 180 ;
			fUp.x = fUp.width + 1; 
			fUp.y = fUp.height ;
			
			var fOver:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			fOver.rotation = 180 ;
			fOver.x = fUp.width + 1; 
			fOver.y = fUp.height ;
			
			var fDown:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			fDown.rotation = 180 ;
			fDown.x = fUp.width + 1; 
			fDown.y = fUp.height ;
			
			fBtn = new WakSimpleButton( fUp, fOver, fDown);
			fBtn.y = 2 ;
			fBtn.enabled = false ;
			return fBtn ;
		}
		
		protected function CreateDownArrow():WakSimpleButton
		{
			var fUp:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			var fOver:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			var fDown:Sprite = DrawDownArrow(mArrowBGColor,mArrowColor);
			var fBtn:WakSimpleButton = new WakSimpleButton(fUp,fOver,fDown);
			
			fBtn.y = fBtn.height + 2 ;
			fBtn.enabled = false ;
			return fBtn;
			  
		}
		
		private var mDiffColorUp:uint = 0x1F1F1F ;
		private var mDiffColorOver:uint = 0x2F2F2F ;
		private var mDefaultButtonW:int = 40 ;
		
		protected function CreateOkButton():WakSimpleButton
		{
			var fButton:WakSimpleButton = null ;
			var fBtnW:int = mDefaultButtonW ;
			
			var fDiffColorUp:uint = mDiffColorUp ;
			var fDiffColorOver:uint = mDiffColorOver ;
			
			var fUp:Sprite = new Sprite();
			DrawDefaultButtonBg(fUp, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			DrawOkIcon(fUp);
			
			var fOver:Sprite = new Sprite();
			DrawDefaultButtonBg(fOver, fBtnW, fBtnW, mBgColor - fDiffColorOver);
			DrawOkIcon(fOver);
			
			var fDown:Sprite = new Sprite();
			DrawDefaultButtonBg(fDown, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			DrawOkIcon(fDown);
			
			fButton = new WakSimpleButton( fUp, fOver, fDown, "");
			
			//fButton.filters = [mBevelFilter1] ;
			
			return fButton ;
		}
		
		protected function CreateActionButton(pAction:String):WakSimpleButton
		{
			var fButton:WakSimpleButton = null ;
			var fBtnW:int = mDefaultButtonW ;
			
			var fDiffColorUp:uint = mDiffColorUp ;
			var fDiffColorOver:uint = mDiffColorOver ;
			
			var fUp:Sprite = new Sprite();
			DrawDefaultButtonBg(fUp, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			SciGui.DrawActionIcon(fUp,pAction);
			
			var fOver:Sprite = new Sprite();
			DrawDefaultButtonBg(fOver, fBtnW, fBtnW, mBgColor - fDiffColorOver);
			SciGui.DrawActionIcon(fOver,pAction);
			
			var fDown:Sprite = new Sprite();
			DrawDefaultButtonBg(fDown, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			SciGui.DrawActionIcon(fDown,pAction);
			
			fButton = new WakSimpleButton( fUp, fOver, fDown, pAction);
			
			//fButton.filters = [mBevelFilter1] ;
			
			return fButton ;
		}
		
		protected function CreateSelectButton():WakSimpleButton
		{
			var fButton:WakSimpleButton = null ;
			var fBtnW:int = mDefaultButtonW ;
			
			var fDiffColorUp:uint = mDiffColorUp ;
			var fDiffColorOver:uint = mDiffColorOver ;
			
			var fUp:Sprite = new Sprite();
			DrawDefaultButtonBg(fUp, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			SciGui.DrawActionIcon(fUp,"");
			
			var fOver:Sprite = new Sprite();
			DrawDefaultButtonBg(fOver, fBtnW, fBtnW, mBgColor - fDiffColorOver);
			SciGui.DrawActionIcon(fOver,"");
			
			var fDown:Sprite = new Sprite();
			DrawDefaultButtonBg(fDown, fBtnW, fBtnW, mBgColor - fDiffColorUp);
			SciGui.DrawActionIcon(fDown,"");
			
			fButton = new WakSimpleButton( fUp, fOver, fDown, "");
			
			//fButton.filters = [mBevelFilter1] ;
			
			return fButton ;
		}
		
		protected function DrawOkIcon(pBtn:Sprite):void
		{
			pBtn.graphics.beginFill(0x00BB00);
			pBtn.graphics.drawCircle( pBtn.width / 2, pBtn.height / 2, 10);
			pBtn.graphics.endFill() ;	
		}
		
		
		
		protected function DrawDefaultButtonBg(pBtn:Sprite, pW:Number, pH:Number, pColor:uint):void
		{
			pBtn.graphics.beginFill(pColor);
			pBtn.graphics.drawRoundRect(0,0,pW,pH,10,10);
			pBtn.graphics.endFill();
		}
		
		protected function DrawDownArrow(pBgColor:uint, pArrowColor:uint):Sprite
		{
			var fArrow:Sprite = new Sprite();
			fArrow.graphics.beginFill(pBgColor);
			fArrow.graphics.drawRoundRect(0,0,26,44,10,10);
			fArrow.graphics.endFill();
			
			fArrow.graphics.lineStyle(1,pArrowColor);
			fArrow.graphics.moveTo(9,2);
			fArrow.graphics.lineTo(9,25);
			
			fArrow.graphics.moveTo(9,25);
			fArrow.graphics.lineTo(1,25);
			
			fArrow.graphics.moveTo(1,25);
			fArrow.graphics.lineTo(13,38);
			
			fArrow.graphics.moveTo(13,38);
			fArrow.graphics.lineTo(26,25);
			
			fArrow.graphics.moveTo(26,25);
			fArrow.graphics.lineTo(18,25);
			
			fArrow.graphics.moveTo(18,25);
			fArrow.graphics.lineTo(18,2);
			
			fArrow.graphics.moveTo(18,2);
			fArrow.graphics.lineTo(9,2);
			fArrow.graphics.endFill();
			
			return fArrow ;
			
		}
		
		protected function OnScrollDown(event:Event):void
		{
			//trace("OnScrollDown");
			if( !(mArrowDown.enabled) )
			{
				return ;
			}
			
			ScrollDown();
			
			
		}
		
		protected function OnScrollUp(event:Event):void
		{
			//trace("OnScrollUp");
			if( !(mArrowUp.enabled) )
			{
				return ;
			}
			
			ScrollUp();
		}
		
		protected function ScrollDown():void
		{
			mScrollingLayer.y -= mItemHeight + mRowsSeparator ;
			
			UpdateScrollButtons();
			
		}
		
		protected function UpdateScrollButtons():void
		{
			var fLastItem:DisplayObject = mItemsArray[mItemsArray.length -1] ;
			
			var fLastItemRow:int = (mScrollingLayer.y + fLastItem.y) / (mItemHeight + mColsSeparator) ;
			if( fLastItemRow >= mRows )
			{
				mArrowDown.enabled = true ;
				
			}
			else
			{
				mArrowDown.enabled = false ;
			}
			
			var fFirstItem:DisplayObject = mItemsArray[0] ;
			
			if( (fFirstItem.y + mScrollingLayer.y) < mInventoryMask.y )
			{
				mArrowUp.enabled = true ;
			}
			else if((fFirstItem.y + mScrollingLayer.y) >= 0 )
			{
				mArrowUp.enabled = false ;
			}
			
		}
		
		protected function ScrollUp():void
		{
			mScrollingLayer.y += mItemHeight + mRowsSeparator ;
			UpdateScrollButtons();
		}
		
		
		
		protected function OnActionSelected(event:Event):void
		{
			//trace("OnActionSelected");
			
			var fButton:WakSimpleButton = event.currentTarget as WakSimpleButton ;
			
			if(fButton == null)
				return ;
			
			mItemWasSelected = false ;
			if( fButton != mSelectButton)
			{
				mSelectingItem = false ;
				// dispatch the action event.
				DispatchAction(fButton.buttonName);
			}
			else
			{
				mMouseCursor.SetDefault() ;
				mSelectingItem = true ;
			}
		}
		
		protected function DispatchAction(pActionId:String, pItem:InventoryItem = null):void
		{
			//trace("SciInventory DispatchAction:"+pActionId+" "+pItem );
			var fNewEvent:Event = new GuiEvent(GuiEvent.ACTION_SELECTED, pActionId,pItem);
			
			if(pActionId == AdventureActions.ActionIdUse)
			{
				// if an action is active, the item selection is disabled.
				mSelectingItem = false ;
				
			}
			
			mLastActionDispatched = pActionId ;
			this.dispatchEvent(fNewEvent);
		}
		
		
		
	}
}